<html>
<head><meta charset="utf-8"><title>Comparing slice of usize is ~700% slower than slice of u8 · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html">Comparing slice of usize is ~700% slower than slice of u8</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220733600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220733600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220733600">(Dec 22 2020 at 20:58)</a>:</h4>
<p>Given the following code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">criterion</span>::<span class="p">{</span><span class="n">black_box</span><span class="p">,</span><span class="w"> </span><span class="n">criterion_group</span><span class="p">,</span><span class="w"> </span><span class="n">criterion_main</span><span class="p">,</span><span class="w"> </span><span class="n">Criterion</span><span class="p">};</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">cmp</span>::<span class="n">Ordering</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[inline(never)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">cmp_u8</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">],</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nc">Ordering</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[inline(never)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">cmp_usize</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">usize</span><span class="p">],</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">usize</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nc">Ordering</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">criterion_benchmark</span><span class="p">(</span><span class="n">c</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">Criterion</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">453342564535342</span><span class="k">usize</span><span class="p">;</span><span class="w"> </span><span class="mi">4096</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">aa</span>: <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">4096</span><span class="o">*</span><span class="mi">8</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="n">a</span><span class="p">)};</span><span class="w"></span>

<span class="w">    </span><span class="n">c</span><span class="p">.</span><span class="n">bench_function</span><span class="p">(</span><span class="s">"cmp_u8"</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">b</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">b</span><span class="p">.</span><span class="n">iter</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">black_box</span><span class="p">(</span><span class="n">cmp_u8</span><span class="p">(</span><span class="o">&amp;</span><span class="n">aa</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">aa</span><span class="p">)))</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="n">c</span><span class="p">.</span><span class="n">bench_function</span><span class="p">(</span><span class="s">"cmp_usize"</span><span class="p">,</span><span class="w"> </span><span class="o">|</span><span class="n">b</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">b</span><span class="p">.</span><span class="n">iter</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">black_box</span><span class="p">(</span><span class="n">cmp_usize</span><span class="p">(</span><span class="o">&amp;</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">)))</span><span class="w"></span>
<span class="w">    </span><span class="p">});</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">criterion_group</span><span class="o">!</span><span class="p">(</span><span class="n">benches</span><span class="p">,</span><span class="w"> </span><span class="n">criterion_benchmark</span><span class="p">);</span><span class="w"></span>
<span class="n">criterion_main</span><span class="o">!</span><span class="p">(</span><span class="n">benches</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>I get:</p>
<div class="codehilite"><pre><span></span><code>cmp_u8                  time:   [611.93 ns 639.82 ns 671.31 ns]

cmp_usize               time:   [4.3133 us 4.4363 us 4.5769 us]
</code></pre></div>
<p>I assumed they will at least have similar performance, if not a little better for usize (as it's aligned correctly)<br>
at the very least on LE platforms we can use memcmp for the usize comparisons</p>



<a name="220735933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220735933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220735933">(Dec 22 2020 at 21:26)</a>:</h4>
<p>You can get syntax highlighting with </p>
<div class="codehilite"><pre><span></span><code>```rust
</code></pre></div>



<a name="220736039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736039">(Dec 22 2020 at 21:28)</a>:</h4>
<p>Comparison has to... compare; what is the performance of equality testing?</p>



<a name="220736069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736069">(Dec 22 2020 at 21:28)</a>:</h4>
<p>You have a <code>cmp_usize2</code> that isn't used</p>



<a name="220736094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736094">(Dec 22 2020 at 21:29)</a>:</h4>
<p>Yeah I had a mistake in the example, fixed now</p>



<a name="220736512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736512">(Dec 22 2020 at 21:33)</a>:</h4>
<p>You want to try something like this? (untested)</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">compare</span><span class="p">(</span><span class="n">a</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">],</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">ah</span><span class="p">,</span><span class="w"> </span><span class="n">ab</span><span class="p">,</span><span class="w"> </span><span class="n">at</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">align_to</span>::<span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">bh</span><span class="p">,</span><span class="w"> </span><span class="n">bb</span><span class="p">,</span><span class="w"> </span><span class="n">bt</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">align_to</span>::<span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">ah</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">bh</span><span class="p">).</span><span class="n">then_with</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">ab</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">bb</span><span class="p">)).</span><span class="n">then_with</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">at</span><span class="p">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">bt</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="220736595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736595">(Dec 22 2020 at 21:34)</a>:</h4>
<p>try to align the data to usize, compare it as a large chunk, handling the head and tail</p>



<a name="220736615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736615">(Dec 22 2020 at 21:35)</a>:</h4>
<p>It's the opposite, u8 compares way <em>faster</em> than usize here</p>



<a name="220736631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736631">(Dec 22 2020 at 21:35)</a>:</h4>
<p>notice that u8 is in nano seconds while usize is in micro seconds</p>



<a name="220736632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736632">(Dec 22 2020 at 21:35)</a>:</h4>
<p>ah, the units are important to read</p>



<a name="220736695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736695">(Dec 22 2020 at 21:36)</a>:</h4>
<p>Then my guess is that <code>&amp;[u8]</code> has a tuned assembly implementation</p>



<a name="220736718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736718">(Dec 22 2020 at 21:36)</a>:</h4>
<p>try my code, flipping the <code>usize</code> and <code>u8</code></p>



<a name="220736741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736741">(Dec 22 2020 at 21:36)</a>:</h4>
<p>well... is that valid...</p>



<a name="220736752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736752">(Dec 22 2020 at 21:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220736695">said</a>:</p>
<blockquote>
<p>Then my guess is that <code>&amp;[u8]</code> has a tuned assembly implementation</p>
</blockquote>
<p>I wonder how tuned can it be. and if we can't beat it then we should probably transmute to [u8] on LE platforms</p>



<a name="220736779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736779">(Dec 22 2020 at 21:37)</a>:</h4>
<p>Well, when you get to bytes, then you can start leveraging SIMD and friends</p>



<a name="220736806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736806">(Dec 22 2020 at 21:37)</a>:</h4>
<p>e.g <a href="https://docs.rs/jetscii/0.4.4/jetscii/#benchmarks">a 10x speedup</a></p>



<a name="220736897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736897">(Dec 22 2020 at 21:39)</a>:</h4>
<p>I assumed that every bytes comparing ASM starts with getting rid of unaligned stuff, and then does aligned/SIMD reads and comparisons.<br>
so ideally usize compare can do the same but skip the "getting rid of unaligned stuff" phase</p>



<a name="220736934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736934">(Dec 22 2020 at 21:40)</a>:</h4>
<p>do you think a PR that transmutes to [u8] on LE on Ord/Eq impls for u16/u32/u64/u128/usize will get accepted?</p>



<a name="220736983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220736983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220736983">(Dec 22 2020 at 21:40)</a>:</h4>
<p>You know what they say about assuming.</p>



<a name="220737006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737006">(Dec 22 2020 at 21:40)</a>:</h4>
<p>You can add an <code>assert!(head.is_empty())</code> which gets optimized to nothing, in tests I've done.</p>



<a name="220737019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737019">(Dec 22 2020 at 21:41)</a>:</h4>
<p>in this case it does seem to actually be true though: <a href="https://github.com/esmil/musl/blob/master/src/string/memcpy.c">https://github.com/esmil/musl/blob/master/src/string/memcpy.c</a></p>



<a name="220737022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737022">(Dec 22 2020 at 21:41)</a>:</h4>
<p>that way you can write the safer-looking code but not actuallyhave any overhead</p>



<a name="220737066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737066">(Dec 22 2020 at 21:41)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> I'm not following the connection to mem<strong>cpy</strong> here.</p>



<a name="220737080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737080">(Dec 22 2020 at 21:41)</a>:</h4>
<p>oh oops</p>



<a name="220737132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737132">(Dec 22 2020 at 21:42)</a>:</h4>
<p>lol yeah memcmp is not optimized at all in musl <a href="https://github.com/esmil/musl/blob/master/src/string/memcmp.c">https://github.com/esmil/musl/blob/master/src/string/memcmp.c</a></p>



<a name="220737148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737148">(Dec 22 2020 at 21:42)</a>:</h4>
<p>probably glibc does magical things but I've never found it worth my time to try and struggle through glibc</p>



<a name="220737152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737152">(Dec 22 2020 at 21:42)</a>:</h4>
<p>On LE, you'd compare the low order bytes first with memcmp. Isn't that wrong?</p>



<a name="220737203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737203">(Dec 22 2020 at 21:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220737148">said</a>:</p>
<blockquote>
<p>probably glibc does magical things but I've never found it worth my time to try and struggle through glibc</p>
</blockquote>
<p>glibc does seem to do that for memcmp <a href="https://sourceware.org/git/?p=glibc.git;a=blob;f=string/memcmp.c#l117">https://sourceware.org/git/?p=glibc.git;a=blob;f=string/memcmp.c#l117</a></p>



<a name="220737347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737347">(Dec 22 2020 at 21:45)</a>:</h4>
<p>wow, for glibc that's positively readable lol</p>



<a name="220737370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737370">(Dec 22 2020 at 21:45)</a>:</h4>
<p>I've been scarred by <a href="https://drewdevault.com/2020/09/25/A-story-of-two-libcs.html">https://drewdevault.com/2020/09/25/A-story-of-two-libcs.html</a></p>



<a name="220737390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737390">(Dec 22 2020 at 21:45)</a>:</h4>
<p>I still prefer reading Rust though <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="220737466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737466">(Dec 22 2020 at 21:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="255061">Léo Lanteri Thauvin</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220737390">said</a>:</p>
<blockquote>
<p>I still prefer reading Rust though <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>
</blockquote>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">asm!</span><span class="p">(</span><span class="n">include_str</span><span class="p">(</span><span class="s">"glibc/memcmp.asm"</span><span class="p">));</span><span class="w"></span>
</code></pre></div>



<a name="220737501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737501">(Dec 22 2020 at 21:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220737466">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="255061">Léo Lanteri Thauvin</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220737390">said</a>:</p>
<blockquote>
<p>I still prefer reading Rust though <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>
</blockquote>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">asm!</span><span class="p">(</span><span class="n">include_str</span><span class="p">(</span><span class="s">"glibc/memcmp.asm"</span><span class="p">));</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>Didn't you forget a <code>!</code> here? x)</p>



<a name="220737536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737536">(Dec 22 2020 at 21:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="306073">Tyson Nottingham</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220737152">said</a>:</p>
<blockquote>
<p>On LE, you'd compare the low order bytes first with memcmp. Isn't that wrong?</p>
</blockquote>
<p>wops. I knew something was fishy hehe, in all my tests I tested a slice against itself so I haven't realized that I thought of endianess backwards <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span> (middle of the night here so I have an excuse <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span> )</p>



<a name="220737623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737623">(Dec 22 2020 at 21:48)</a>:</h4>
<p>That conclusively settles it — BE is objectively better.</p>



<a name="220737797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737797">(Dec 22 2020 at 21:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220737536">said</a>:</p>
<blockquote>
<p>(middle of the night here so I have an excuse <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span> )</p>
</blockquote>
<p>That endianness is involved is excuse enough <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="220737813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737813">(Dec 22 2020 at 21:51)</a>:</h4>
<p>so I guess time to go study glibc memcmp implementation and come up with one for u16/u32/u64/usize and PR that? <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> <span aria-label="joy" class="emoji emoji-1f602" role="img" title="joy">:joy:</span></p>



<a name="220737903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220737903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220737903">(Dec 22 2020 at 21:52)</a>:</h4>
<p>May I suggest having a good night of sleep before that? xD</p>



<a name="220738016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220738016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220738016">(Dec 22 2020 at 21:53)</a>:</h4>
<p>Or else... nightmares.</p>



<a name="220738091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220738091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220738091">(Dec 22 2020 at 21:54)</a>:</h4>
<p><a href="https://www.youtube.com/watch?v=HNt0sTlhTYE">https://www.youtube.com/watch?v=HNt0sTlhTYE</a></p>
<div class="youtube-video message_inline_image"><a data-id="HNt0sTlhTYE" href="https://www.youtube.com/watch?v=HNt0sTlhTYE"><img src="https://i.ytimg.com/vi/HNt0sTlhTYE/default.jpg"></a></div>



<a name="220738823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220738823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220738823">(Dec 22 2020 at 22:04)</a>:</h4>
<p>Also, probably some(a lot?) of the perf doesn't come from their C impl, but from their ASM impl <a href="https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/memcmp.S">https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/memcmp.S</a></p>



<a name="220762204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Comparing%20slice%20of%20usize%20is%20%7E700%25%20slower%20than%20slice%20of%20u8/near/220762204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8.html#220762204">(Dec 23 2020 at 05:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Comparing.20slice.20of.20usize.20is.20~700.25.20slower.20than.20slice.20of.20u8/near/220736595">said</a>:</p>
<blockquote>
<p>try to align the data to usize, compare it as a large chunk, handling the head and tail</p>
</blockquote>
<p>Note that the <code>align_to</code>-twice version given is incorrect -- it'll give different prefix and suffix lengths if the arrays are differently aligned, and thus will compare incorrectly for those cases.</p>
<p>(This could certainly be optimized like <a href="https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/core/ptr/fn.swap_nonoverlapping_bytes.html">https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/core/ptr/fn.swap_nonoverlapping_bytes.html</a> is, though.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>